package com.pks.insurance.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.pks.insurance.helper.AdminHelper;

/**
 * Perform activities for managing users Servlet implementation class mainly
 * used to delete users
 * 
 * @author Pankaj Soni
 */
public class AdminServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6322263000809554300L;
	private static final Logger LOGGER = Logger.getLogger(AdminServlet.class);

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected final void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LOGGER.debug("begin");
		String task = request.getParameter("task");
		String ssn = request.getParameter("ssn");
		LOGGER.info("task: " + task + " ssn: " + ssn);
		AdminHelper adminHelper = new AdminHelper();
		if ("delete-user".equals(task)) {
			adminHelper.deleteUser(ssn);
			LOGGER.info("Users deleted: ");
			request.setAttribute("message", " User is deleted ");
		} else if ("search-user".equals(task)) {

			// Retrive list of all the users matching given ssn
			if ("".equals(ssn)) {
				request.setAttribute("customers",
						new AdminHelper().getUsersList());
			}
			request.setAttribute("customers",
					new AdminHelper().getUsersList(ssn));
			request.getRequestDispatcher("adminManage.jsp").forward(request,
					response);
			return;
		}

		// Retrive list of all the users
		request.setAttribute("customers", new AdminHelper().getUsersList());
		request.getRequestDispatcher("adminManage.jsp").forward(request,
				response);
		LOGGER.debug("end");
	}

}
